﻿/*----------------------------------------------------------------
  //Copyright (C) 2012 赵国伟
  //文件功能描述：广告管理
  //创建人：赵国伟
  //生成时间：2012/05/12 12:15:00 （汶川地震4年）
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Shopping.Common;
using Shopping.Model;
using Shopping.DAL;

namespace Shopping.BLL
{
    public class ADManage
    {
        private readonly DALADs dalAD = new DALADs();
        private readonly DALADContents dalContent = new DALADContents();


        #region "查询系统中的广告信息； by 赵国伟 2012-05-12"
        /// <summary>
        /// 查询系统中的广告信息；
        /// </summary>
        /// <returns></returns>
        public List<ADs> QueryList()
        {
            return dalAD.QueryList("", "CreateDate desc");
        }
        #endregion

        #region "保存广告信息； by 赵国伟 2012-05-12"
        /// <summary>
        /// 保存广告信息；
        /// </summary>
        /// <param name="ad"></param>
        /// <param name="contents"></param>
        /// <returns></returns>
        public ReturnValue Save(AdInfo a)
        {
            ReturnValue ret = new ReturnValue();
            if (dalAD.QueryList(1, "ADName='" +a.AD.ADName + "' and ADID<>" + a.AD.ADID, "") != null)
            {
                ret.Message = "广告名称重复！";
                return ret;
            }
            a.AD.CreateDate = DateTime.Now;
            a.AD.LastModifyDate = DateTime.Now;
            if (a.AD.ADID > 0)
            {
                ADs o = dalAD.GetModel(a.AD.ADID);
                a.AD.CreateDate = o.CreateDate;
            }

            foreach (ADContents c in a.Contents)
            {
                c.ADPicUrl = FilesManage.MoveFileByUrl(c.ADPicUrl, "AD");
            }

            return dalAD.Save(a.AD,a.Contents);
        }
        #endregion

        #region "删除指定的广告； by 赵国伟 2012-05-12"
        /// <summary>
        /// 删除指定的广告；
        /// </summary>
        /// <param name="ADID"></param>
        /// <returns></returns>
        public ReturnValue Delete(int ADID)
        {
            ReturnValue ret = new ReturnValue(ResultType.Fail, "删除失败");
            if (dalAD.Delete(ADID) > 0)
            {
                ret.Result = ResultType.Success;
                ret.Message = "删除成功！";
            }
            return ret;
        }
        #endregion

        #region "查询一条广告信息； by 赵国伟 2012-05-12"
        /// <summary>
        /// 查询一条广告信息；
        /// </summary>
        /// <param name="adID"></param>
        /// <returns></returns>
        public AdInfo GetAD(int adID)
        {
            AdInfo a = null;
            ADs ad = dalAD.GetModel(adID);
            if (ad != null)
            {
                a = new AdInfo();
                a.AD = ad;
                a.Contents = dalContent.QueryList("ADID=" + adID, "ADContentID");
            }
            return a;
        }
        #endregion


    }
}
